﻿using System;
using System.Text;

namespace Core.Library
{
    public class PagerUtility
    {
        public static string PageLinks(int pageIndex, int pageSize, int totalItems, int numPage, string linkFormat)
        {
            int totalPages = (int)Math.Ceiling((decimal)totalItems / pageSize);

            if (totalPages == 1) return "";
            var result = new StringBuilder();

            if (totalPages > 0)
            {
                if (pageIndex > 1)
                {
                    var tagBack = new TagBuilder("a");
                    int pageback = pageIndex - 1;
                    tagBack.MergeAttribute("href", string.Format(linkFormat, pageback.ToString()));
                    tagBack.MergeAttribute("class", "prevbutn");
                    //tagBack.InnerHtml = "&laquo;";
                    result.AppendLine(tagBack.ToString());
                }
                if (numPage > 0)
                {
                    if (totalPages <= numPage)
                    {
                        for (int i = 1; i <= totalPages; i++)
                        {
                            var tag = new TagBuilder("a"); // Construct an <a> tag 

                            tag.MergeAttribute("href", string.Format(linkFormat, i.ToString()));

                            tag.InnerHtml = i.ToString();
                            if (i == pageIndex)
                                tag.MergeAttribute("class", "active");
                            result.AppendLine(tag.ToString());
                        }
                    }
                    else
                    {
                        for (int i = 0; i <= numPage; i++)
                        {
                            int j = i + (((int)pageIndex / numPage) * numPage);
                            if (j > totalPages) break;
                            if (j != 0)
                            {
                                TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag

                                tag.MergeAttribute("href", string.Format(linkFormat, j.ToString()));
                                tag.InnerHtml = j.ToString();

                                if (j == pageIndex)
                                    tag.MergeAttribute("class", "active");
                                result.AppendLine(tag.ToString());
                            }
                        }
                    }
                }
                if (pageIndex < totalPages)
                {
                    var tagNext = new TagBuilder("a");
                    int pagenext = pageIndex + 1;

                    tagNext.MergeAttribute("href", string.Format(linkFormat, pagenext.ToString()));
                    tagNext.MergeAttribute("class", "nextbutn");
                    //tagNext.InnerHtml = "&raquo;";
                    result.AppendLine(tagNext.ToString());
                }

            }
            return result.ToString();
        }
    }
}
